package chat.client.controller;

import chat.client.service.ClientService;
import chat.client.view.ClientView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 客户端事件控制类
 *
 * #1 获取到客户端内的按钮点击事件、窗体关闭事件
 * #2 将事件的类型分发到对应的业务类中的对应方法内
 *
 * @author 谈笑、
 * @dateTime 2020/10/21 15:41
 */
public class ClientController {

	// 窗体对象
	private ClientView clientView;

	// 业务对象
	private ClientService clientService;

	public ClientController() {}

	/**
	 * 构造方法初始化成员属性
	 */
	public ClientController(ClientView clientView, ClientService clientService) {
		this.clientView = clientView;
		this.clientService = clientService;
	}

	/**
	 * 消息操作按钮事件
	 */
	public void clientButtonEvent_1() {
		// 发送报文
		this.clientView.getjButtons()[0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.sendMsg(clientView);
			}
		});

		// 复制内容
		this.clientView.getjButtons()[1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.copyMsg(clientView);
			}
		});

		// 清空内容
		this.clientView.getjButtons()[2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.emptyMsg(clientView);
			}
		});
	}

	/**
	 * 配置服务按钮事件
	 */
	public void clientButtonEvent_2() {
		// 连接服务
		this.clientView.getjButtons()[3].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.connectServer(clientView);
			}
		});

		// 断开连接
		this.clientView.getjButtons()[4].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.breakConnect();
			}
		});

		// 退出程序
		this.clientView.getjButtons()[5].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clientService.clientSignOut(clientView);
			}
		});
	}

	/**
	 * 关闭窗体事件
	 */
	public void closeClientWindow() {
		this.clientView.getjFrame().addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				clientService.clientSignOut(clientView);
			}
		});
	}

}